F week 8 FabAcademy 2016

WEEK eight

Assignment for this week is, learn to analyze and understand data sheet of a microcontroller (in my case I am using Atmel ATtiny44A) And program the replicated "hello world board" with the LED and a SWITCH (Show the replicated hello world board in my week six )




What i am going to do :

  • Reading and learn ATtiny 44 microcontroller datasheet.
  • Program the ATtiny 44 microcontroller on my board
  • Described the programming process/es I used

Reading Datasheet



I began by looking through the Ttiny44 microcontroller's datasheet and trying to comprehend what the Attiny44's datasheet is about; it was an annoying in the beginning. The most interesting thing in the whole datasheet is the association of the pins to their coding representative numbers. The datasheet is also telling you where you can place components if there are special "needs" (for example analog input)

pins configurations

There I found the necessary information on the pins that I needed later on when programming

"

For Six -Wire ISP Programming of ATtiny 44 using six pins shown in below

  • SCK(Serial Clock): Programming clock, generated by the In-System Programmer (Master)

  • MOSI(Master Out - Slave In ): Communication line from In-System Programmer (Master) to target AVR being programmed (Slave )

  • MISO( Master In - Slave Out ): Communication line from target AVR (Slave) to In- System Programmer (Master)

  • RST(Reset): To enable In-System Programming, the target AVR Reset must be kept active. To simplify this, the In-System Programmer should control the target AVR Reset

  • GND(Ground): Common Ground








What is memory &Registers ?

Memory is an area where code and instructions are stored.There are normally 3 types of memory present in a microcontroller. These are SRAM, FLASH, and EEPROM memories.

  • SRAM is the type of memory where data must be read and written to repeatedly. This is the data will change the different code being uploaded to the AVR microcontroller circuit. By default, this is the most common and used type of memory.


  • Flash memory is the memory that normally stores data that does not change. This is the program memory. It stores the part of the microcontroller program that is fixed and will always stay permanent. This is similar to the BIOS of a general-purpose computer.




  • The Flash memory has an endurance of at least 10,000 write/erase cycles. The ATtiny24A/44A/84A Program Counter (PC) is 10/11/12 bits wide, thus addressing the 1024/2048/4096 Program memory locations

Registers are small memory elements in microcontrollers with 8 bits capacity. Registers can be accessed quickly by the ALU (Arithmetic and Logic Unit) of microcontrollers









Other useful information







ATtiny pins and Arduino pins are the same?

I thought that the Attiny pin and arduino pin was the same ,and recognized it NOT!. In this week I am using Arduino IDE to program the Attiny44, so I compare Attiny44 pins with Arduino pins



How to installing ATtiny support in Arduino ?

ATtiny can program in different methods. in this week I am using ATtin44. So I first studied about how to program ATtiny using Arduino IDE

  • Open the preferences in the "File" > "preferences" menu.

  • Find the "Additional Boards Manager URLs field near the bottom of the dialog.(shown in below )

  • Copy and paste the following URL into the field (use a comma to separate it from any URLs you've already added):

  • https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json i>








  • Click the OK button to save your updated preferences.

  • Open the boards manager in the "Tools > Board" menu. Scroll to the bottom of the list; you should see an entry for "ATtiny".Click on the ATtiny entry. An install button should appear. Click the install button.The word "installed" should now appear next to the title of the ATtiny entry.






Close the boards manager. You should now see an entry for ATtiny in the "Tools > Board" menu

Program the board using Arduino IDE



First, I installed all software and configure drivers for program the hello world board, then I connected fabISP to my computer using the USB cable and then connected my hello world board to the Fab






An AVR microcontroller you just bought will not work on your Arduino. It needs to be the bootloader for Arduino IDE The tutorial explains how to do it on your ATtiny44A.
On the Arduino IDE, First, I select board type as ATtiny. Then selected microcontroller used on my board (in my case ATtiny44).
Here no need to selected port at serial port (port is automatically select ), this indicate port of PC to communicate with a microcontroller on the board.


Then Make the following changes in Tools menu

Board > ATtiny

Processor > ATtiny44

Clock > 20 MHZ(because i am using 20 MHZ Resonator in my Board)






Then i selected "USBtinyISP" at Programer. Then Programmer the bootloader to the board



For checking the board , I opened the blink example sketch and made some changes on pin (change pin number 13 to 2) configuration and then compiled it then loaded to my circuit board
For reference, use schematics and layout of board from week 6

void setup() {
// initialize digital pin 2 as an output.
pinMode(2, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(2, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}



it worked just fine





I wrote a code for control two LED with one button using Arduino IDE.

int switchPin = 7; // switch is connected to pin 7
int led1Pin = 2;
int led2pin = 3;
int val; // variable for reading the pin status
int val2; // variable for reading the delayed status
int buttonState; // variable to hold the button state
int Mode = 0; // What mode is the light in?
void setup() {
pinMode(switchPin, INPUT); // Set the switch pin as input
pinMode(led1Pin, OUTPUT);
pinMode(led2pin, OUTPUT);
buttonState = digitalRead(switchPin); // read the initial state
}
void loop(){
val = digitalRead(switchPin); // read input value and store it in val
delay(10); // 10 milliseconds is a good amount of time
val2 = digitalRead(switchPin); // read the input again to check for bounces
if (val == val2) // make sure we got 2 consistant readings!
{
if (val != buttonState) // the button state has changed!
{
if (val == LOW) // check if the button is pressed
{
if (Mode == 0) {
Mode = 1;
} else {
if (Mode == 1) {
Mode = 2;
} else {
if (Mode == 2) {
Mode = 3;
} else {
if (Mode == 3) {
Mode = 0;
}
}
}
}
}
}
buttonState = val; // save the new state in our variable
}
// Now do whatever the lightMode indicates
if (Mode == 0) { // all-off
digitalWrite(led1Pin, LOW);
digitalWrite(led2pin, LOW);
}
if (Mode == 1) {
digitalWrite(led1Pin, HIGH);
digitalWrite(led2pin, LOW);
}
if (Mode == 2) {
digitalWrite(led1Pin, LOW);
digitalWrite(led2pin, HIGH);
}
if (Mode == 3) {
digitalWrite(led1Pin, HIGH);
digitalWrite(led2pin, HIGH);
}
}



Final result show in video




Assembly programming

I don't have previous experience with Assembly programming; now I start to learn the Assembly .I got this from a nice and helpfull tutorial for those who are familiar with C,